class Student(object):
    # 学生信息类
    def __init__(self, num='', name='', gender='', score={"语文": 0, "数学": 0, "英语": 0}, total=0, rank=0):
        self.__num = num  # 学号
        self.__name = name  # 姓名
        self.__gender = gender  # 性别
        self.__score = score  # 3门课成绩
        self.__total = total  # 总分
        self.__rank = rank  # 名次

    def __str__(self):
        s = "{:<8}{:<8}{:<8}{:<8}{:<8}{:<8}{:<8}{:<8}".format(
            self.__num, self.__name, self.__gender,
            self.__score["语文"], self.__score["数学"], self.__score["英语"],
            self.__total, self.__rank)
        return s

    def getNum(self):
        return self.__num

    def getName(self):
        return self.__name

    def getGender(self):
        return self.__gender

    def getScore(self):
        return self.__score

    def getTotal(self):
        return self.__total

    def getRank(self):
        return self.__rank

    def calcTotal(self):
        self.__total = sum(self.__score.values())

    def calcRank(self, scores):
        count = 1
        for score in scores:
            if score > self.__total:
                count += 1
        self.__rank = count


def readStu(stuList, n=20):
    # 读入学生记录值，学号为0或读满规定条数记录时停止
    while n > 0:
        print("请输入一个学生的详细信息（学号为0时结束输入）：")
        num = input("学号：")  # 输入学号
        if num == '0':  # 学号为0停止输入
            break
        else:
            if searchStu(stuList, num, "学号") != []:
                # 学号相同不允许插入，保证学号的唯一性
                print("列表中存在相同的学号，禁止插入！")
                return len(stuList)
        name = input("姓名：")  # 输入名字
        gender = input("性别：")  # 输入性别
        score = {}  # 创建空字典用于存放三门课的成绩
        print("请输入该学生三门课程的成绩，用空格分割：")
        score["语文"], score["数学"], score["英语"] = map(float, input().split())
        oneStu = Student(num, name, gender, score)
        stuList.append(oneStu)
        n = n - 1
    return len(stuList)  # 返回实际读入的记录条数


def printStu(stuList):
    # 输出所有学生记录的值
    for stu in stuList:
        print(stu)


def searchStu(stuList, keyword, condition):
    result = []
    for i in range(len(stuList)):
        if condition == "学号" and stuList[i].getNum() == keyword:
            result.append(i)
        elif condition == "姓名" and stuList[i].getName() == keyword:
            result.append(i)
        elif condition == "排名" and stuList[i].getRank() == int(keyword):
            result.append(i)
        elif condition == "总分" and stuList[i].getTotal() == float(keyword):
            result.append(i)
    return result


def deleteStu(stuList, num):  # 从列表中删除指定学号的一个元素
    for stu in stuList:  # 寻找待删除的元素
        if stu.getNum() == num:  # 如果找到相等元素
            stuList.remove(stu)  # 删除对应的元素
            print("已删除指定学号的学生信息")
            break
    else:  # 如果找不到待删除的元素
        print("该学生不存在，删除失败！")  # 给出提示信息后返回


def compare(s1, s2, condition):
    # 根据condition条件比较两个student记录的大小
    if condition == "学号":
        return s1.getNum() > s2.getNum()
    if condition == "姓名":
        return s1.getName() > s2.getName()
    if condition == "总分":
        return s1.getTotal() > s2.getTotal()


def sortStu(stuList, condition):  # 选择法排序，按condition条件由小到大排序
    for i in range(0, len(stuList) - 1):  # 控制循环的n-1趟
        minpos = i  # minpos用来存储本趟最小元素所在下标
        for j in range(i + 1, len(stuList)):  # 寻找本趟最小元素所在的下标
            if compare(stuList[minpos], stuList[j], condition):
                minpos = j
        if i != minpos:  # 保证本趟最小元素到达下标为i的位置
            stuList[i], stuList[minpos] = stuList[minpos], stuList[i]
